package com.gxa.service.impl.cm;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.mapper.EmplMapper;
import com.gxa.mapper.cm.DevplanMapper;
import com.gxa.param.cm.QueryParam;
import com.gxa.pojo.Empl;
import com.gxa.pojo.cm.Devplan;
import com.gxa.pojo.cm.vo.TableVO;
import com.gxa.service.cm.DevplanService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author Glenn
 */
@Service
public class DevplanServiceImpl implements DevplanService {

    @Resource
    DevplanMapper devplanMapper;

    @Resource
    EmplMapper emplMapper;

    @Override
    public Devplan getById(Integer id) {
        return devplanMapper.getById(id);
    }

    @Override
    public PageInfo<Devplan> listDevplan(QueryParam queryParam) {
        PageHelper.startPage(queryParam.getPage(), queryParam.getLimit());
        List<Devplan> devplans = devplanMapper.listDevplan(queryParam);
        return new PageInfo<>(devplans);
    }

    @Override
    public TableVO addDevplan(Devplan devplan, String name, HttpSession session) {
        String emplName = (String) session.getAttribute("emplName");
        Empl empl = emplMapper.findByName(emplName);
        Empl createEmp = emplMapper.findByName(name);
        devplan.setCreateEmp(createEmp);
        devplan.setUpdateEmp(empl);
        devplan.setCreateTime(new Timestamp(System.currentTimeMillis()));
        devplan.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        devplanMapper.insertDevplan(devplan);
        return new TableVO().setCode(200).setMsg("添加成功");
    }

    @Override
    public TableVO updateDevplan(Devplan devplan, String name, HttpSession session) {
        Devplan dbDevplan = devplanMapper.getById(devplan.getId());
        String emplName = (String) session.getAttribute("emplName");
        Empl empl = emplMapper.findByName(emplName);
        Empl createEmp = emplMapper.findByName(name);

        dbDevplan.setNo(devplan.getNo());
        dbDevplan.setStatus(devplan.getStatus());
        dbDevplan.setContent(devplan.getContent());
        dbDevplan.setCreateEmp(createEmp);
        dbDevplan.setUpdateEmp(empl);
        dbDevplan.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        devplanMapper.updateDevplan(dbDevplan);
        return new TableVO().setCode(200).setMsg("修改成功");
    }

    @Override
    public R batchDelete(List<Integer> ids) {
        devplanMapper.batchDelete(ids);
        return new R(200, "删除成功！");
    }
}
